package com.maimao.spider.pipeline;

import com.maimao.spider.mapper.CategoryMapper;
import com.maimao.spider.model.Category;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.util.List;
import java.util.Map;

/**
 * @author MaoLin Wang
 * @date 2019/11/2421:14
 */
@Component
@Slf4j
public class CategoryPipeline implements Pipeline {

    @Autowired
    private CategoryMapper categoryMapper;

//    @Autowired
//    private PlatformTransactionManager platformTransactionManager;
//
//    @Autowired
//    private TransactionDefinition transactionDefinition;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void process(ResultItems resultItems, Task task) {
        synchronized (CategoryPipeline.class) {
            Category cate1 = resultItems.get("cate1");
            if (cate1 != null) {

                Integer cateId = categoryMapper.insert(cate1);
                List<Category> cate2List = resultItems.get("cate2");
                Map<Integer, List<Category>> cate3Map = resultItems.get("cate3");

                for (int i = 0; i < cate2List.size(); i++) {
                    Category cate2 = cate2List.get(i);
                    cate2.setPid(cate1.getId());

                    Integer cate2Id = categoryMapper.insert(cate2);
                    List<Category> cate3List = cate3Map.get(i);
                    Integer finalCate2Id = cate2.getId();
                    cate3List.stream().forEach(cate3 -> {
                        cate3.setPid(finalCate2Id);
                    });
                    categoryMapper.insertBatch(cate3List);
                }
                log.info("插入成功：一级分类：{}\n二级分类：{}\n三级分类：{}",cate1,cate2List,cate3Map);
            }
        }
    }
   /* @Override
    public void process(ResultItems resultItems, Task task) {
        synchronized (CategoryPipeline.class){
            Category cate1 = resultItems.get("cate1");
            if (cate1 != null) {

                TransactionStatus transaction = platformTransactionManager.getTransaction(transactionDefinition);
                Integer cateId=null;
                try {
                    cateId = categoryMapper.insert(cate1);
                } catch (Exception e) {
                    platformTransactionManager.rollback(transaction);
                }
                platformTransactionManager.commit(transaction);
                List<Category> cate2List = resultItems.get("cate2");
                Map<Integer, List<Category>> cate3Map = resultItems.get("cate3");

                for (int i = 0; i < cate2List.size(); i++) {
                    Category cate2 = cate2List.get(i);
                    transaction=platformTransactionManager.getTransaction(transactionDefinition);
                    cate2.setPid(cateId);

                    Integer cate2Id=null;
                    try{
                        cate2Id = categoryMapper.insert(cate2);
                    }catch (Exception e) {
                        platformTransactionManager.rollback(transaction);
                    }
                    platformTransactionManager.commit(transaction);
                    List<Category> cate3List = cate3Map.get(i);
                    Integer finalCate2Id = cate2Id;
                    cate3List.stream().forEach(cate3 -> {
                        cate3.setPid(finalCate2Id);
                    });
                    transaction=platformTransactionManager.getTransaction(transactionDefinition);
                    try{
                        categoryMapper.insertBatch(cate3List);
                    }catch (Exception e) {
                        platformTransactionManager.rollback(transaction);
                    }
                    platformTransactionManager.commit(transaction);

                }


                log.info("=============================================================================" + cate1.getName());
            }
        }
    }*/
}
